/*
 * jQuery blockUI plugin
 * Version 2.02 (04/07/2008)
 * @requires jQuery v1.2.3 or later
 *
 * Examples at: http://malsup.com/jquery/block/
 * Copyright (c) 2007-2008 M. Alsup
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Special thanks to Amir-Hossein Sobhi for some excellent contributions!
 */

(function($) {

if (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery)) {
    alert('blockUI requires jQuery v1.2.3 or later!  You are using v' + $.fn.jquery);
    return;
}

// global $ methods for blocking/unblocking the entire page
$.blockUI   = function(opts) { install(window, opts); };
$.unblockUI = function(opts) { remove(window, opts); };

// plugin method for blocking element content
$.fn.block = function(opts) {
    return this.each(function() {
        if ($.css(this,'position') == 'static')
            this.style.position = 'relative';
        if ($.browser.msie) 
            this.style.zoom = 1; // force 'hasLayout'
        install(this, opts);
    });
};

// plugin method for unblocking element content
$.fn.unblock = function(opts) {
    return this.each(function() {
        remove(this, opts);
    });
};

$.blockUI.version = 2.02; // 2nd generation blocking at no extra cost!

// override these in your code to change the default behavior and style
$.blockUI.defaults = {
    // message displayed when blocking (use null for no message)
    message:  '<h1>Please wait...</h1>',
    
    // styles for the message when blocking; if you wish to disable
    // these and use an external stylesheet then do this in your code:
    // $.blockUI.defaults.css = {};
    css: { 
        padding:        0,
        margin:         0,
        width:          '30%', 
        top:            '40%', 
        left:           '35%', 
        textAlign:      'center', 
        color:          '#fff', 
        border:         '1px solid #ccc',
        backgroundColor:'#fff',
        cursor:         'wait'
    },
    
    // styles for the overlay
    overlayCSS:  { 
        backgroundColor:'#000', 
        opacity:        '0.1' 
    },
    
    // allow body element to be stetched in ie6; this makes blocking look better
    // on "short" pages.  disable if you wish to prevent changes to the body height
    allowBodyStretch: true,
    
    // be default blockUI will supress tab navigation from leaving blocking content;
    constrainTabKey: true,
    
    // fadeOut time in millis; set to 0 to disable fadeout on unblock
    fadeOut:  400,
    
    // suppresses the use of overlay styles on FF/Linux (due to significant performance issues with opacity)
    applyPlatformOpacityRules: true
};

// private data and functions follow...

var ie6 = $.browser.msie && /MSIE 6.0/.test(navigator.userAgent);
var pageBlock = null;
var pageBlockEls = [];

function install(el, opts) {
    var full = (el == window);
    var msg = opts && opts.message !== undefined ? opts.message : undefined;
    opts = $.extend({}, $.blockUI.defaults, opts || {});
    opts.overlayCSS = $.extend({}, $.blockUI.defaults.overlayCSS, opts.overlayCSS || {});
    var css = $.extend({}, $.blockUI.defaults.css, opts.css || {});
    msg = msg === undefined ? opts.message : msg;

    // remove the current block (if there is one)
    if (full && pageBlock) 
        remove(window, {fadeOut:0}); 
    
    // if an existing element is being used as the blocking content then we capture
    // its current place in the DOM (and current display style) so we can restore
    // it when we unblock
    if (msg && typeof msg != 'string' && (msg.parentNode || msg.jquery)) {
        var node = msg.jquery ? msg[0] : msg;
        var data = {};
        $(el).data('blockUI.history', data);
        data.el = node;
        data.parent = node.parentNode;
        data.display = node.style.display;
        data.parent.removeChild(node);
    }
    
    // blockUI uses 3 layers for blocking, for simplicity they are all used on every platform;
    // layer1 is the iframe layer which is used to supress bleed through of underlying content
    // layer2 is the overlay layer which has opacity and a wait cursor
    // layer3 is the message content that is displayed while blocking

    var lyr1 = ($.browser.msie) ? $('<iframe class="blockUI" style="z-index:1000;border:none;margin:0;padding:0;position:absolute;width:100%;height:100%;top:0;left:0" src="javascript:false;"></iframe>')
                                : $('<div class="blockUI" style="display:none"></div>');
    var lyr2 = $('<div class="blockUI" style="z-index:1001;border:none;margin:0;padding:0;width:100%;height:100%;top:0;left:0"></div>');
    var lyr3 = full ? $('<div id="blockMsgs" class="blockUI blockMsg blockPage" style="z-index:1002;position:fixed"></div>')
                    : $('<div id="blockMsgs" class="blockUI blockMsg blockElement" style="z-index:1002;display:none;position:absolute"></div>');

    // if we have a message, style it
    if (msg) 
        lyr3.css(css);

    // style the overlay
    if (!opts.applyPlatformOpacityRules || !($.browser.mozilla && /Linux/.test(navigator.platform))) 
        lyr2.css(opts.overlayCSS);
    lyr2.css('position', full ? 'fixed' : 'absolute');
    
    // make iframe layer transparent in IE
    if ($.browser.msie) 
        lyr1.css('opacity','0.0');

    $([lyr1[0],lyr2[0],lyr3[0]]).appendTo(full ? 'body' : el);

    // ie7 must use absolute positioning in quirks mode and to account for activex issues (when scrolling)
    var expr = $.browser.msie && (!$.boxModel || $('object,embed', full ? null : el).length > 0);
    if (ie6 || expr) {
        // give body 100% height
        if (full && opts.allowBodyStretch && $.boxModel)
            $('html,body').css('height','100%');

        // fix ie6 issue when blocked element has a border width
        if ((ie6 || !$.boxModel) && !full) {
            var t = sz(el,'borderTopWidth'), l = sz(el,'borderLeftWidth');
            var fixT = t ? '(0 - '+t+')' : 0;
            var fixL = l ? '(0 - '+l+')' : 0;
        }

        // simulate fixed position
        $.each([lyr1,lyr2,lyr3], function(i,o) {
            var s = o[0].style;
            s.position = 'absolute';
            if (i < 2) {
                full ? s.setExpression('height','document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + "px"')
                     : s.setExpression('height','this.parentNode.offsetHeight + "px"');
                full ? s.setExpression('width','jQuery.boxModel && document.documentElement.clientWidth || document.body.clientWidth + "px"')
                     : s.setExpression('width','this.parentNode.offsetWidth + "px"');
                if (fixL) s.setExpression('left', fixL);
                if (fixT) s.setExpression('top', fixT);
            }
            else {
                if (full) s.setExpression('top','(document.documentElement.clientHeight || document.body.clientHeight) / 2 - (this.offsetHeight / 2) + (blah = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "px"');
                s.marginTop = 0;
            }
        });
    }
    
    // show the message
    lyr3.append(msg).show();
    if (msg && (msg.jquery || msg.nodeType))
        $(msg).show();

    // bind key and mouse events
    bind(1, el, opts);
        
    if (full) {
        pageBlock = lyr3[0];
        pageBlockEls = $(':input:enabled:visible',pageBlock);
        setTimeout(focus, 20);
    }
    else 
        center(lyr3[0]);
};

// remove the block
function remove(el, opts) {
    var full = el == window;
    var data = $(el).data('blockUI.history');
    opts = $.extend(true, {}, $.blockUI.defaults, opts);
    bind(0, el, opts); // unbind events
    var els = full ? $('body > .blockUI') : $('.blockUI', el);
    if (full) 
        pageBlock = pageBlockEls = null;

    if (opts.fadeOut) {
        els.fadeOut(opts.fadeOut);
        setTimeout(function() { reset(els,data); }, opts.fadeOut);
    }
    else
        reset(els, data);
};

// move blocking element back into the DOM where it started
function reset(els,data) {
    els.each(function(i,o) {
        // remove via DOM calls so we don't lose event handlers
        if (this.parentNode) 
            this.parentNode.removeChild(this);
    });
    if (data) {
        data.el.style.display = data.display;
        data.parent.appendChild(data.el);
        $(data.el).removeData('blockUI.history');
    }
};

// bind/unbind the handler
function bind(b, el, opts) {
    var full = el == window, $el = $(el);
    
    // don't bother unbinding if there is nothing to unbind
    if (!b && (full && !pageBlock || !full && !$el.data('blockUI.isBlocked'))) 
        return;
    if (!full) 
        $el.data('blockUI.isBlocked', b);
        
    // bind anchors and inputs for mouse and key events
    var events = 'mousedown mouseup keydown keypress click';
    b ? $(document).bind(events, opts, handler) : $(document).unbind(events, handler);

// former impl...
//    var $e = $('a,:input');
//    b ? $e.bind(events, opts, handler) : $e.unbind(events, handler);
};

// event handler to suppress keyboard/mouse events when blocking
function handler(e) {
    // allow tab navigation (conditionally)
    if (e.keyCode && e.keyCode == 9) {
        if (pageBlock && e.data.constrainTabKey) {
            var els = pageBlockEls;
            var fwd = !e.shiftKey && e.target == els[els.length-1];
            var back = e.shiftKey && e.target == els[0];
            if (fwd || back) {
                setTimeout(function(){focus(back)},10);
                return false;
            }
        }
    }
    // allow events within the message content
    if ($(e.target).parents('div.blockMsg').length > 0)
        return true;
        
    // allow events for content that is not being blocked
    return $(e.target).parents().children().filter('div.blockUI').length == 0;
};

function focus(back) {
    if (!pageBlockEls) 
        return;
    var e = pageBlockEls[back===true ? pageBlockEls.length-1 : 0];
    if (e) 
        e.focus();
};

function center(el) {
    var p = el.parentNode, s = el.style;
    var l = ((p.offsetWidth - el.offsetWidth)/2) - sz(p,'borderLeftWidth');
    var t = ((p.offsetHeight - el.offsetHeight)/2) - sz(p,'borderTopWidth');
    s.left = l > 0 ? (l+'px') : '0';
    s.top  = t > 0 ? (t+'px') : '0';
};

function sz(el, p) { 
    return parseInt($.css(el,p))||0; 
};

})(jQuery);



/*
 * Tinybox，未成功，日后调试
 *第一个参数是要显示的 AJAX 或 HTML 内容
 *第二个参数是设置是否为 AJAX
 *第三个参数是宽度，0 为自动
 *第四个参数是高度，0 为自动
 *第五个参数是是否设置隐藏
 *第六个参数是设置是否自动隐藏的时间
 */
var TINYbox = function(){
	this.initialize();
}

TINYbox.prototype.initialize = function(){
	this.p = 0;
	this.m = 0;
	this.b = 0;
	this.fn = 0;
	this.ic = 0;
	this.iu = 0;
	this.iw = 0;
	this.ih = 0;
	this.ia = 0;
	this.f = 0;
}

TINYbox.prototype.show = function(c,u,w,h,a,t) {
	if(!this.f){
		this.p=document.createElement('div'); this.p.id='TinyBox0';
		this.m=document.createElement('div'); this.m.id='TinyMask0';
		this.b=document.createElement('div'); this.b.id='TinyContent0';
		document.body.appendChild(this.m);
		document.body.appendChild(this.p);
		this.p.appendChild(this.b);
		window.onresize = this.resize();
		this.f = 1;
		//this.m.onclick = this.hide();
	}

	//CSS
	document.getElementById('TinyBox0').style.cssText = "position:absolute; display:none; padding:0; background:#fff; z-index:2000";
	document.getElementById('TinyMask0').style.cssText = "position:absolute; display:none; top:0; left:0; height:100%; width:100%; background:#e6e6e6; z-index:1500";
	document.getElementById('TinyContent0').style.cssText = "background:#fff";
	
	if(!a && !u){
		this.p.style.width = w ? w + 'px' : 'auto'; 
		this.p.style.height = h ? h + 'px' : 'auto';
		this.p.style.backgroundImage = 'none'; 
		this.b.innerHTML = c;
	}else{
		this.b.style.display = 'none'; 
		this.p.style.width = this.p.style.height='100px';
	}
	this.mask();
	this.ic = c; this.iu = u; this.iw = w; this.ih = h; this.ia = a; 
	this.alpha(this.m, 1, 80, 3);
	if(t){
		var self = this;
		setTimeout(function(){self.hide()},1000*t);
	}
}

TINYbox.prototype.fill = function(c,u,w,h,a){
	if(u){
		this.p.style.backgroundImage = '';
		var x = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
		x.onreadystatechange = function(){
			if(x.readyState == 4 && x.status == 200){this.psh(x.responseText,w,h,a)}
		};
		x.open('GET', c, 1); 
		x.send(null);
	}else{
		this.psh(c, w, h, a);
	}
}

TINYbox.prototype.psh = function(c,w,h,a){
	if(a){
		if(!w||!h){
			var x = this.p.style.width;
			var y = this.p.style.height; 
			this.b.innerHTML = c;
			this.p.style.width = w ? w + 'px' : '';
			this.p.style.height = h ? h + 'px' : '';
			this.b.style.display = '';
			w = parseInt(this.b.offsetWidth); 
			h = parseInt(this.b.offsetHeight);
			this.b.style.display = 'none';
			this.p.style.width = x; 
			this.p.style.height = y;
		}else{
			this.b.innerHTML = c;
		}
		this.size(this.p, w, h);
	}else{
		this.p.style.backgroundImage='none';
	}
}

TINYbox.prototype.hide = function(){
	this.alpha(this.p, -1, 0, 3);
},
TINYbox.prototype.resize = function(){
	this.pos(); 
	this.mask();
}

TINYbox.prototype.mask = function(){
	this.m.style.height = this.page.total(1) + 'px';
	this.m.style.width = ''; 
	this.m.style.width = this.page.total(0) + 'px';
}

TINYbox.prototype.pos = function(){
	var t = (this.page.height() / 2) - (this.p.offsetHeight / 2); 
	t = t < 10 ? 10 : t;
	this.p.style.top = (t + this.page.top()) + 'px';
	this.p.style.left = (this.page.width() / 2) - (this.p.offsetWidth / 2) + 'px';
}

TINYbox.prototype.alpha = function(e,d,a){
	clearInterval(e.ai);
	if(d == 1){
		e.style.opacity = 0; 
		e.style.filter = 'alpha(opacity=0)';
		e.style.display = 'block'; 
		this.pos();
	}
	var self = this;
	e.ai = setInterval(function(){self.ta(e,a,d)}, 80);
}

TINYbox.prototype.ta = function(e,a,d){
	var o = Math.round(e.style.opacity * 100);
	if(o == a){
		clearInterval(e.ai);
		if(d == -1){
			e.style.display = 'none';
			e == this.p ? this.alpha(this.m,-1,0,2) : this.b.innerHTML = this.p.style.backgroundImage = '';
		}else{
			e == this.m ? this.alpha(this.p,1,100) : this.fill(this.ic, this.iu, this.iw, this.ih, this.ia);
		}
	}else{
		var n = Math.ceil((o + ((a - o) * .9))); 
		n = n == 1 ? 0 : n;
		e.style.opacity = n / 100; 
		e.style.filter = 'alpha(opacity=' + n + ')';
	}
}

TINYbox.prototype.size = function(e,w,h){
	e = typeof e == 'object' ? e : document.getElementById(e); 
	clearInterval(e.si);
	var ow = e.offsetWidth;
	var oh = e.offsetHeight;
	wo = ow - parseInt(e.style.width), ho = oh - parseInt(e.style.height);
	var wd = ow - wo > w ? 0 : 1, hd = (oh - ho > h) ? 0 : 1;
	var self = this;
	e.si = setInterval(function(){self.ts(e,w,wo,wd,h,ho,hd)},20)
}
		
TINYbox.prototype.ts = function(e,w,wo,wd,h,ho,hd){
	var ow = e.offsetWidth - wo, oh = e.offsetHeight - ho;
	if(ow == w && oh == h){
		clearInterval(e.si); 
		this.p.style.backgroundImage = 'none'; 
		this.b.style.display = 'block';
	}else{
		if(ow != w){
			var n = ow + ((w - ow) * .5); 
			e.style.width = wd ? Math.ceil(n) + 'px' : Math.floor(n)+'px';
		}
		if(oh != h){
			var n = oh + ((h - oh) * .5); 
			e.style.height = hd ? Math.ceil(n) + 'px' : Math.floor(n) + 'px';
		}
		this.pos();
	}
}


TINYbox.prototype.page = function(){
	return{
		top:function(){return document.documentElement.scrollTop||document.body.scrollTop},
		width:function(){return self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth},
		height:function(){return self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight},
		total:function(d){
			var b=document.body, e=document.documentElement;
			return d?Math.max(Math.max(b.scrollHeight,e.scrollHeight),Math.max(b.clientHeight,e.clientHeight)):
			Math.max(Math.max(b.scrollWidth,e.scrollWidth),Math.max(b.clientWidth,e.clientWidth))
		}
	}
}();



/*
 * Tinybox
 *第一个参数是要显示的 AJAX 或 HTML 内容
 *第二个参数是设置是否为 AJAX
 *第三个参数是宽度，0 为自动
 *第四个参数是高度，0 为自动
 *第五个参数是是否设置隐藏
 *第六个参数是设置是否自动隐藏的时间
 */

var TINY={};

TINY.box=function(){
	var p,m,b,fn,ic,iu,iw,ih,ia,f=0;
	return{
		show:function(c,u,w,h,a,t){
			if(!f){
				p=document.createElement('div'); p.id='TinyBox0';
				m=document.createElement('div'); m.id='TinyMask0';
				b=document.createElement('div'); b.id='TinyContent0';
				document.body.appendChild(m);
				document.body.appendChild(p);
				p.appendChild(b);
				//m.onclick=TINY.box.hide;
				window.onresize=TINY.box.resize;
				f=1;
			}

			//CSS
			document.getElementById('TinyBox0').style.cssText = "position:absolute; display:none; padding:0; background:#fff; z-index:2000";
			document.getElementById('TinyMask0').style.cssText = "position:absolute; display:none; top:0; left:0; height:100%; width:100%; background:#e6e6e6; z-index:1500";
			document.getElementById('TinyContent0').style.cssText = "background:#fff";
			
			if(!a&&!u){
				p.style.width=w?w+'px':'auto'; p.style.height=h?h+'px':'auto';
				p.style.backgroundImage='none'; b.innerHTML=c
			}else{
				b.style.display='none'; p.style.width=p.style.height='100px'
			}
			this.mask();
			ic=c; iu=u; iw=w; ih=h; ia=a; this.alpha(m,1,80,3);
			if(t){setTimeout(function(){TINY.box.hide()},1000*t)}
		},
		fill:function(c,u,w,h,a){
			if(u){
				p.style.backgroundImage='';
				var x=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
				x.onreadystatechange=function(){
					if(x.readyState==4&&x.status==200){TINY.box.psh(x.responseText,w,h,a)}
				};
				x.open('GET',c,1); x.send(null)
			}else{
				this.psh(c,w,h,a)
			}
		},
		psh:function(c,w,h,a){
			if(a){
				if(!w||!h){
					var x=p.style.width, y=p.style.height; b.innerHTML=c;
					p.style.width=w?w+'px':''; p.style.height=h?h+'px':'';
					b.style.display='';
					w=parseInt(b.offsetWidth); h=parseInt(b.offsetHeight);
					b.style.display='none'; p.style.width=x; p.style.height=y;
				}else{
					b.innerHTML=c
				}
				this.size(p,w,h)
			}else{
				p.style.backgroundImage='none'
			}
		},
		hide:function(){
			TINY.box.alpha(p,-1,0,3);
		},
		resize:function(){
			TINY.box.pos(); TINY.box.mask()
		},
		mask:function(){
			m.style.height=TINY.page.total(1)+'px';
			m.style.width=''; m.style.width=TINY.page.total(0)+'px';
		},
		pos:function(){
			var t=(TINY.page.height()/2)-(p.offsetHeight/2); t=t<10?10:t;
			p.style.top=(t+TINY.page.top())+'px';
			p.style.left=(TINY.page.width()/2)-(p.offsetWidth/2)+'px';
		},
		alpha:function(e,d,a){
			clearInterval(e.ai);
			if(d==1){
				e.style.opacity=0; e.style.filter='alpha(opacity=0)';
				e.style.display='block'; this.pos()
			}
			e.ai=setInterval(function(){TINY.box.ta(e,a,d)},80)
		},
		ta:function(e,a,d){
			var o=Math.round(e.style.opacity*100);
			if(o==a){
				clearInterval(e.ai);
				if(d==-1){
					e.style.display='none';
					e==p?TINY.box.alpha(m,-1,0,2):b.innerHTML=p.style.backgroundImage=''
				}else{
					e==m?this.alpha(p,1,100):TINY.box.fill(ic,iu,iw,ih,ia)
				}
			}else{
				var n=Math.ceil((o+((a-o)*.9))); n=n==1?0:n;
				e.style.opacity=n/100; e.style.filter='alpha(opacity='+n+')'
			}
		},
		size:function(e,w,h){
			e=typeof e=='object'?e:T$(e); clearInterval(e.si);
			var ow=e.offsetWidth, oh=e.offsetHeight,
			wo=ow-parseInt(e.style.width), ho=oh-parseInt(e.style.height);
			var wd=ow-wo>w?0:1, hd=(oh-ho>h)?0:1;
			e.si=setInterval(function(){TINY.box.ts(e,w,wo,wd,h,ho,hd)},20)
		},
		ts:function(e,w,wo,wd,h,ho,hd){
			var ow=e.offsetWidth-wo, oh=e.offsetHeight-ho;
			if(ow==w&&oh==h){
				clearInterval(e.si); p.style.backgroundImage='none'; b.style.display='block'
			}else{
				if(ow!=w){var n=ow+((w-ow)*.5); e.style.width=wd?Math.ceil(n)+'px':Math.floor(n)+'px'}
				if(oh!=h){var n=oh+((h-oh)*.5); e.style.height=hd?Math.ceil(n)+'px':Math.floor(n)+'px'}
				this.pos()
			}
		}
	}
}();

TINY.page=function(){
	return{
		top:function(){return document.documentElement.scrollTop||document.body.scrollTop},
		width:function(){return self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth},
		height:function(){return self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight},
		total:function(d){
			var b=document.body, e=document.documentElement;
			return d?Math.max(Math.max(b.scrollHeight,e.scrollHeight),Math.max(b.clientHeight,e.clientHeight)):
			Math.max(Math.max(b.scrollWidth,e.scrollWidth),Math.max(b.clientWidth,e.clientWidth))
		}
	}
}();




/*
 * Draggable
 * 
 */

/*
Example:
[CSS]
#element_id {
  position: absolute;
  top: 10px;
  left: 35px;
  width: 150px;
  height: 100px;
  background-color: #00b;
  cursor: move;
}
[HTML]
<div id="element_id"></div>
[JavaScript]
var draggable = new Draggable("element_id");
*/



/**
 * Constructor
 * @param {String|Element} element - element id or element object
 */
var Draggable = function(element, TIT) {
  this.initialize(element,TIT);
}

/**
 * remove EventListeners
 */
Draggable.prototype.destroy = function() {
  this.detach(this.element, "mousedown", this.observers["mousedown"]);
  this.detach(this.html, "mouseup", this.observers["mouseup"]);
  this.detach(this.html, "mousemove", this.observers["mousemove"]);
}

Draggable.prototype.initialize = function(element, TIT) {
  moveTIT = document.getElementById(TIT);
  this.isIE = navigator.appVersion.lastIndexOf("MSIE") > 0;
  this.isFF = navigator.userAgent.toLowerCase().indexOf("firefox") > 0;
  this.html = document.getElementsByTagName("html").item(0);
  if (typeof element == "string") {
    this.element = document.getElementById(element);
  } else {
    this.element = element;
  }
  this.ww = document.documentElement.clientWidth||document.body.clientWidth;
  this.style = this.element.style;
  this.thisBaseX;
  this.thisBaseY;
  this.pageBaseX;
  this.pageBaseY;
  this.scrollBaseY;
  this.isMoving = false;
  this.observers = {};
  var self = this;
  // Mousedown
  this.observers["mousedown"] = this.observe(moveTIT, "mousedown", function(event) {
    if (self.isMoving) return;
    event = event || window.event;
    self.disableSelect(event);
    var position = self.getPosition();
    self.thisBaseX = position["x"];
    self.thisBaseY = position["y"];
    self.pageBaseX = event.pageX || event.clientX;
    self.pageBaseY = event.pageY || event.clientY;
    if (self.isIE) self.scrollBaseY = document.body.scrollTop;
    self.isMoving = true;
  });
  // Mousemove
  this.observers["mousemove"] = this.observe(this.html, "mousemove", function(event) {
    if (!self.isMoving) return;
    event = event || window.event;
    var x = (event.pageX || event.clientX) - self.pageBaseX + self.thisBaseX;
    var y = (event.pageY || event.clientY) - self.pageBaseY + self.thisBaseY;
    if (self.isIE) y += (parseInt(document.body.scrollTop) - self.scrollBaseY);    
    if(x > self.ww - self.element.offsetWidth) x = self.ww - self.element.offsetWidth;
    self.setPosition(x, y);
  });
  // Mouseup
  this.observers["mouseup"] = this.observe(this.html, "mouseup", function(event) {
    if (!self.isMoving) return;
    self.enableSelect();
    self.isMoving = false;
  });
}

Draggable.prototype.observe = function(element, name, observer) {
  if (element.addEventListener) {
    element.addEventListener(name, observer, false);
  } else if (element.attachEvent) {
    element.attachEvent("on" + name, observer);
  }
  return observer;
}

Draggable.prototype.detach = function(element, name, observer) {
  if (element.removeEventListener) {
    element.removeEventListener(name, observer, false);
  } else if (element.detachEvent) {
    try {
      element.detachEvent("on" + name, observer);
    } catch (e) {}
  }
}

Draggable.prototype.setPosition = function(x, y) {
  this.style.left = x + "px";
  this.style.top = y + "px";
}

Draggable.prototype.getPosition = function() {
  var x, y;
  // First you acquire from css style information.
  if (this.style.top == "" || this.style.left == "") {
    if (this.element.currentStyle) {
      x = this.element.currentStyle["left"];
      y = this.element.currentStyle["top"];
    } else {
      var computedStyle = document.defaultView.getComputedStyle(this.element, null);
      x = computedStyle["left"];
      y = computedStyle["top"];
    }
  } else {
    x = this.style.left;
    y = this.style.top;
  }
  return {"x": parseInt(x.replace("px", "")) || 0, "y": parseInt(y.replace("px", "")) || 0};
}

Draggable.prototype.disableSelect = function(event) {
  if (this.isIE) {
    document.getElementsByTagName("body").item(0).onselectstart = function(e){ return false };
  } else {
    try { event.preventDefault(); } catch(e) {}
  }
}

Draggable.prototype.enableSelect = function() {
  if (this.isIE) document.getElementsByTagName("body").item(0).onselectstart = "";
}


